<?php
/**
 * @title 332. 重新安排行程 - 优化
 * @author start2004
 */

ini_set("memory_limit", "512M");


class Solution {

    /**
     * @param String[][] $tickets
     * @return String[]
     */
    function findItinerary($tickets) {
        /**
         * @since 2020-09-01 最大深度
         */
        $maxlen = count($tickets)+1;

        /**
         * @since 2020-08-31 出发地
         */
        $depArray = [];

        /**
         * @since 2020-08-31 迭代
         */
        foreach ($tickets as list($dep, $arr)){
            $depArray[$dep][] = $arr;
        }

        /**
         * @since 2020-08-31 排序
         */
        foreach ($depArray as $dep=>$arrArray){
            rsort($arrArray);
            $depArray[$dep] = $arrArray;
        }
        // return $depArray;

        /**
         * @since 2020-08-31 JFK开始
         */
        list($result, $tmp) = self::find($depArray, "JFK", $maxlen);
        $result = array_reverse($result);

        /**
         * @return
         */
        return $result;
    }

    /**
     * @title 递归查找
     * @author start2004
     * @since 2020-08-31 Created
     *
     * @param array $depArray
     * @param string $dep
     * @param int $len
     */
    public function find($depArray = [], $dep = ""){
        // echo "dep={$dep}", PHP_EOL;
        // var_dump($depArray);

        /**
         * @since 2020-09-02 存在出发点
         */
        if(isset($depArray[$dep])){
            /**
             * @since 2020-09-02 最后一个字符
             */
            $key = count($depArray[$dep])-1;
            $arr = $depArray[$dep][$key];
            if($key == 0){
                unset($depArray[$dep]);
            } else {
                unset($depArray[$dep][$key]);
            }

            /**
             * @since 2020-09-02 往下查找路径
             */
            list($result, $depArray) = self::find($depArray, $arr);

            /**
             * @since 2020-09-02 如果depArray, 非空, 说明还有行程未使用
             */
            if($depArray AND isset($depArray[$dep])){
                /**
                 * @since 2020-09-02 dep出发下一个点
                 */
                $key = count($depArray[$dep])-1;
                $arr = $depArray[$dep][$key];
                if($key == 0){
                    unset($depArray[$dep]);
                } else {
                    unset($depArray[$dep][$key]);
                }

                /**
                 * @since 2020-09-02 往下查找路径
                 */
                list($result2, $depArray) = self::find($depArray, $arr);
                $result = array_merge($result, $result2);
            } else {}

            /**
             * @since 2020-09-02 增加出发点返回
             */
            $result[] = $dep;
            return [$result, $depArray];
        } else {
            /**
             * @since 2020-09-02 无路可走
             */
            return [[$dep], $depArray];
        }
    }
}


/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.27_2.php
 */
$datas = [
    // [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]],
    // [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]],
    [["JFK","KUL"],["JFK","NRT"],["NRT","JFK"]],
    // [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]],
    // [["EZE","TIA"],["EZE","HBA"],["AXA","TIA"],["JFK","AXA"],["ANU","JFK"],["ADL","ANU"],["TIA","AUA"],["ANU","AUA"],["ADL","EZE"],["ADL","EZE"],["EZE","ADL"],["AXA","EZE"],["AUA","AXA"],["JFK","AXA"],["AXA","AUA"],["AUA","ADL"],["ANU","EZE"],["TIA","ADL"],["EZE","ANU"],["AUA","ANU"]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->findItinerary($data);
    // var_dump($result);
    if(count($result)<=20){
        var_dump($result);
    } else {
        var_dump(count($result));
    }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
